//SPDX-FileCopyrightText: Copyright 2022-2024 深圳市同心圆网络有限公司
//SPDX-License-Identifier: GPL-3.0-only

package widget_dao

import (
	"context"

	"atomgit.com/openlinksaas-org/api-server/dao/db_ns"
	"atomgit.com/openlinksaas-org/proto-gen-go.git/widget_store_api"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
)

type WidgetInfoDbItem struct {
	WidgetId      string   `bson:"_id"`
	WidgetName    string   `bson:"widgetName"`
	ExtensionList []string `bson:"extensionList"`
	FileList      []string `bson:"fileList"`
	FileId        string   `bson:"fileId"`
	IconFileId    string   `bson:"iconFileId"`
	Weight        uint32   `bson:"weight"`
	CreateTime    int64    `bson:"createTime"`
	UpdateTime    int64    `bson:"updateTime"`
}

func (item *WidgetInfoDbItem) ToWidgetInfo() *widget_store_api.WidgetInfo {
	return &widget_store_api.WidgetInfo{
		WidgetId:      item.WidgetId,
		WidgetName:    item.WidgetName,
		ExtensionList: item.ExtensionList,
		FileList:      item.FileList,
		FileId:        item.FileId,
		IconFileId:    item.IconFileId,
		Weight:        item.Weight,
		CreateTime:    item.CreateTime,
		UpdateTime:    item.UpdateTime,
	}
}

type _WidgetDao struct {
	mc *mongo.Collection
}

func newWidgetDao(db *mongo.Database) (*_WidgetDao, error) {
	mc := db.Collection(db_ns.DB_NS_WIDGET_STORE + "info")
	return &_WidgetDao{mc: mc}, nil
}

func (db *_WidgetDao) Insert(ctx context.Context, dbItem *WidgetInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_WidgetDao) Exist(ctx context.Context, widgetId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"_id": widgetId})
	return count > 0, err
}

func (db *_WidgetDao) Get(ctx context.Context, widgetId string) (*WidgetInfoDbItem, error) {
	retItem := &WidgetInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"_id": widgetId}).Decode(retItem)
	return retItem, err
}

func (db *_WidgetDao) Remove(ctx context.Context, widgetId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"_id": widgetId})
	return err
}

func (db *_WidgetDao) List(ctx context.Context) ([]*WidgetInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{}, options.Find().SetSort(bson.M{"createTime": -1}))
	if err != nil {
		return nil, err
	}
	retList := []*WidgetInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_WidgetDao) Update(ctx context.Context, widgetId, widgetName string, extensionList, fileList []string, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": widgetId},
		bson.M{"$set": bson.M{
			"widgetName":    widgetName,
			"extensionList": extensionList,
			"fileList":      fileList,
			"updateTime":    updateTime,
		}})
	return err
}

func (db *_WidgetDao) UpdateIcon(ctx context.Context, widgetId, iconFileId string) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": widgetId},
		bson.M{"$set": bson.M{
			"iconFileId": iconFileId,
		}})
	return err
}

func (db *_WidgetDao) UpdateFile(ctx context.Context, widgetId, fileId string) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": widgetId},
		bson.M{"$set": bson.M{
			"fileId": fileId,
		}})
	return err
}

func (db *_WidgetDao) UpdateWeight(ctx context.Context, widgetId string, weight uint32) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"_id": widgetId},
		bson.M{"$set": bson.M{
			"weight": weight,
		}})
	return err
}
